package com.hgyc.mom.qms.controller;

import java.util.*;

import com.hgyc.mom.wms.vo.WmsPurchaseReceiptVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsIqc;
import com.hgyc.mom.qms.vo.QmsIqcParam;
import com.hgyc.mom.qms.service.QmsIqcService;
import com.hgyc.mom.qms.vo.QmsIqcVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 来料检验单Controller
 *
 * @author fwj
 * @since 2025-09-27
 */
@Tag(name = "来料检验单控制器", description = "来料检验单控制器")
@RestController
@RequestMapping("/qms/iqc")
public class QmsIqcController {
    @Autowired
    private QmsIqcService qmsIqcService;

    @Operation(summary = "查询来料检验单列表", description = "查询来料检验单列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsIqc>> pageList(@Parameter(description = "查询参数") @RequestBody QmsIqcParam qmsIqcParam) {
        Page<QmsIqc> page = new Page<>(qmsIqcParam.getPage(), qmsIqcParam.getPageSize());
        QmsIqc qmsIqc = new QmsIqc();
        BeanUtils.copyProperties(qmsIqcParam, qmsIqc);
        QueryWrapper<QmsIqc> wrapper = QueryWrapperUtil.build(qmsIqc);
        IPage<QmsIqc> list = qmsIqcService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取来料检验单详细信息", description = "获取来料检验单详细信息")
    @GetMapping(value = "/{id}")
    public Result<QmsIqc> getInfo(@Parameter(description = "来料检验单ID") @PathVariable("id") Long id) {
        return Result.success(qmsIqcService.getById(id));
    }
    @Operation(summary = "新增来料检验单", description = "新增来料检验单")
    @PostMapping
    public Result<?> save(@Parameter(description = "来料检验单") @RequestBody QmsIqcVO qmsIqcVO) {
        qmsIqcService.saveIqc(qmsIqcVO);
        return Result.success();
    }

    @Operation(summary = "修改来料检验单", description = "修改来料检验单")
    @PutMapping
    public Result<?> update(@Parameter(description = "来料检验单") @RequestBody QmsIqcVO qmsIqcVO) {
        qmsIqcService.updateIqc(qmsIqcVO);
        return Result.success();
    }

    @Operation(summary = "删除来料检验单", description = "删除来料检验单")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        qmsIqcService.removeIqc(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "来料接收送检", description = "来料接收送检")
    @PostMapping("/inspect")
    public Result<?> inspect(@RequestBody WmsPurchaseReceiptVO wmsPurchaseReceiptVO) {
        qmsIqcService.inspect(wmsPurchaseReceiptVO);
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsIqc> util = new ExcelUtil<>(QmsIqc.class);
        util.importTemplateExcel(response, "来料检验单");
    }

    @Operation(summary = "导出来料检验单列表", description = "导出来料检验单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsIqcParam qmsIqcParam) {
        QmsIqc qmsIqc = new QmsIqc();
        BeanUtils.copyProperties(qmsIqcParam, qmsIqc);
        QueryWrapper<QmsIqc> wrapper = QueryWrapperUtil.build(qmsIqc);
        List<QmsIqc> list = qmsIqcService.list(wrapper);
        ExcelUtil<QmsIqc> util = new ExcelUtil<>(QmsIqc.class);
        util.exportExcel(response, list, "来料检验单数据");
    }

    @Operation(summary = "导入来料检验单列表", description = "导入来料检验单列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsIqc> util = new ExcelUtil<>(QmsIqc.class);
        List<QmsIqc> qmsIqcList = util.importExcel(file.getInputStream());
        String message = qmsIqcService.importData(qmsIqcList, updateSupport);
        return Result.success(message);
    }
}
